package com.ll.mybatis;

import com.ll.mybatis.dao.EmployeeMapper;
import com.ll.mybatis.dao.EmployeeMapperPlus;
import com.ll.mybatis.entity.Department;
import com.ll.mybatis.entity.Employee;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 刘龙
 * @version 1.0
 */
public class MybatisXMLCRUDPlus {
    public SqlSessionFactory getsqlSessionFactory() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        return new SqlSessionFactoryBuilder().build(inputStream);
    }

    /**
     * 查询
     * @throws IOException
     */
    @Test
    public void test1() throws IOException {
        SqlSession sqlSession = getsqlSessionFactory().openSession();

        try {
            EmployeeMapperPlus mapper = sqlSession.getMapper(EmployeeMapperPlus.class);
            Employee empById = mapper.getEmpById(1);
            System.out.println(empById);
        } finally {
            sqlSession.close();
        }

    }

    /**
     * 查询员工信息,与之对应的部门信息也查询出来
     * @throws IOException
     */
    @Test
    public void test2() throws IOException {
        SqlSession sqlSession = getsqlSessionFactory().openSession();

        try {
            EmployeeMapperPlus mapper = sqlSession.getMapper(EmployeeMapperPlus.class);
            Employee employee = mapper.getEmpAndDept(1);
            System.out.println(employee);
        } finally {
            sqlSession.close();
        }
    }

    /**
     * 分步查询
     * @throws IOException
     */
    @Test
    public void test3() throws IOException {
        SqlSession sqlSession = getsqlSessionFactory().openSession();

        try {
            EmployeeMapperPlus mapper = sqlSession.getMapper(EmployeeMapperPlus.class);
            Employee employee = mapper.getEmpByIdStep(1);
            System.out.println(employee);
        } finally {
            sqlSession.close();
        }
    }





}
